﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
//using System.Drawing.Drawing2D;
using iTextSharp.text;
using System.IO;

 
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using System.Threading;
namespace hondaab.admin.articulos_dir
{
    public partial class informes_articulos : System.Web.UI.Page
    {
        public iTextSharp.text.pdf.AcroFields pdfFormFields;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                rellenarpdf("");
            }

        }

        private void rellenarpdf(string fileimagen)
        {
            try
            {
                Document doc = new Document(PageSize.A4);
                //  iTextSharp.text.pdf.PdfReader archivo;
                //    archivo = new iTextSharp.text.pdf.PdfReader(Server.MapPath("./informe-memoria.pdf"));

                if (File.Exists(Server.MapPath("informe.pdf")))
                {
                    File.Delete(Server.MapPath("informe.pdf"));
                }
                PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("informe.pdf"), FileMode.Create));
                //string archivoNuevo = Server.MapPath("informe.pdf");


                doc.Open();
                List<articulos> lista = new List<articulos>();
                lista = articulos.getList();
                int i = 4;

                Chunk chunk1 = new Chunk("Listado Artículos", FontFactory.GetFont("ARIAL", 14, iTextSharp.text.Font.UNDERLINE));
                doc.Add(new Paragraph(chunk1));
                chunk1 = new Chunk(" ", FontFactory.GetFont("ARIAL", 10, iTextSharp.text.Font.UNDERLINE));
                doc.Add(new Paragraph(chunk1));

                int pagina = 1;
                foreach (articulos art in lista)
                {




                    Chunk chunk = new Chunk(art.nombre, FontFactory.GetFont("ARIAL", 10, iTextSharp.text.Font.TIMES_ROMAN));
                    doc.Add(new Paragraph(chunk));
                    //doc.Add(new iTextSharp.text.ListItem(art.nombre));

                    i++;
                    if (i == 50)
                    {
                        chunk = new Chunk("Pagina:" + pagina, FontFactory.GetFont("ARIAL", 10, iTextSharp.text.Font.BOLD));

                        doc.Add(new Paragraph(chunk));
                        i = 1;
                        pagina++;
                    }

                }
                doc.Close();







                //iTextSharp.text.pdf.PdfStamper pdfStamper;


                //  pdfStamper = new iTextSharp.text.pdf.PdfStamper(archivo, new FileStream(archivoNuevo, FileMode.Create));

                //                pdfStamper = new iTextSharp.text.pdf.PdfStamper(archivo, new FileStream(archivoNuevo, FileMode.Create));

                //  pdfFormFields = pdfStamper.AcroFields;

                // para sacar por consola los nombres de los campos...
                //foreach (DictionaryEntry entrada in archivo.AcroFields.Fields)
                //{
                //    HttpContext.Current.Response.Write("ponerValor(" + entrada.Key.ToString()+")  ");
                //}


                //ponerImagen("esquema", fileimagen, ref pdfStamper);
                //ponerValor("acometida2", puntosconexionacometidas.getpuntosconexionacometidas(instax.idPuntoConexionAcometida).nombre);




                //  flatten the form to remove editting options, set it to false
                //  to leave the form open to subsequent manual edits
                //pdfStamper.FormFlattening = false;
                //pdfStamper.Close();


                //string file = (sf.Left(fileimagen, fileimagen.Length - 4) + ".pdf");

                //if (System.IO.File.Exists(Server.MapPath(fileimagen)))
                //    System.IO.File.Delete(Server.MapPath(fileimagen));


            }
            catch (Exception ex)
            {

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            Document document = new Document(PageSize.A4, 50, 50, 50, 50);

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath("inf2.pdf"), FileMode.Create));

            Phrase footPhraseImg = new Phrase("Pag: ", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 8, iTextSharp.text.Font.NORMAL));
            HeaderFooter footer = new HeaderFooter(footPhraseImg, true);
            footer.Border = iTextSharp.text.Rectangle.TOP_BORDER;
            footer.Alignment = Element.ALIGN_RIGHT;
            document.Footer = footer;

            document.Open();

            iTextSharp.text.Image imghead = iTextSharp.text.Image.GetInstance(Server.MapPath("../../images/logo1.gif"));
            imghead.SetAbsolutePosition(0, 0);
            imghead.Alignment = Element.ALIGN_RIGHT;
            PdfContentByte cbhead = writer.DirectContent;
            PdfTemplate tp = cbhead.CreateTemplate(93, 93);
           // tp.AddImage(imghead);
            cbhead.AddTemplate(tp, 260, 580);
            Phrase headPhraseImg = new Phrase(imghead + "", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 7, iTextSharp.text.Font.NORMAL));
            HeaderFooter header = new HeaderFooter(headPhraseImg, true);
            header.Alignment = Element.ALIGN_RIGHT;
            //you can set the font size like this 
            iTextSharp.text.Font font22B = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 20, iTextSharp.text.Font.BOLD);
            iTextSharp.text.Font font12 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLD);

            iTextSharp.text.Font font10 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, iTextSharp.text.Font.NORMAL);

            // add the paragraphs
            Paragraph p1 = new Paragraph("Informe de Artículos ", font22B);
            p1.Alignment = Element.ALIGN_CENTER;
           
            document.Add(new Paragraph(p1));

            p1 = new Paragraph(" ", font22B);
            document.Add(new Paragraph(p1));

            List<articulos> lista = new List<articulos>();
            lista = articulos.getList();



            PdfPTable table = new PdfPTable(4);

            PdfPCell cell = new PdfPCell(new Phrase("dd",font10));

            cell.Colspan = 1;
            
            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right

            //table.AddCell(cell);
            cell = new PdfPCell(new Phrase("Ref.", font12));
            cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);
            cell = new PdfPCell(new Phrase("Nombre.", font12));
            cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);
            cell = new PdfPCell(new Phrase("Precio.", font12));
            cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);
            cell = new PdfPCell(new Phrase("Cantidad.", font12));
            cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);

 
        
            foreach (articulos art in lista)
            {

                cell = new PdfPCell(new Phrase(art.referencia, font10));
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase(art.nombre, font10));
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase(art.precio.ToString() + "€", font10));
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
                cell = new PdfPCell(new Phrase(art.stock.ToString(), font10));
                cell.HorizontalAlignment = 1;
                table.AddCell(cell);
 


                //Paragraph p12 = new Paragraph(art.nombre, font10);
                //p12.Alignment = Element.ALIGN_CENTER;
                //document.Add(new Paragraph(p12));

            }
            document.Add(table);
            //Paragraph p2 = new Paragraph("For ", font22B);
            //p2.Alignment = Element.ALIGN_CENTER;
            //document.Add(new Paragraph(p2));

            //add the table in pdf

            iTextSharp.text.Table table1 = new iTextSharp.text.Table(2, 2);
            table1.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
            table1.Alignment = Element.ALIGN_LEFT;
            table1.WidthPercentage = 30;
            document.Add(table1);

            document.NewPage(); // new page

            document.Close();

            Response.ContentType = "text/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=inf2.pdf");
            Thread.Sleep(5000);
            Response.TransmitFile(Server.MapPath("inf2.pdf"));
            Response.End();

        }

    }
}
